package wiki.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import wiki.generic.contents.Content;
import wiki.generic.io.User;
import wiki.generic.layout.LayoutPrototypeWriter;
import wiki.generic.utilities.PageNotFoundException;
import wiki.setup.Setup;
/**
 * 
 * @author <p><strong>JwTeam</strong></p> 
 *
 */
public class ViewingServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		HttpSession ses = req.getSession(false);
		if (ses != null) {
			String corso=(String) ses.getAttribute("corso");
			boolean isDoc = (Boolean) ses.getAttribute("rights");
			if (isDoc) {
				User us = Setup.getSetup().getMediator().getLoginService()
						.getUser((String) ses.getAttribute("id"));
				if (us.isFirstAccess()) {
					Setup.getSetup().getMediator().getLoginService()
							.setFirstAccess(us.getEmail(), false);
					resp.sendRedirect("./html/configurazione/configurazione.html");
				}

			}

			PrintWriter out = resp.getWriter();
			LayoutPrototypeWriter w = Setup
					.getSetup()
					.getMediator()
					.getVservice()
					.getLayoutWriter(
							Setup.getSetup().getMediator().getVservice()
									.getParameter(corso,"layout"));
			out.write("<html>\n");
			out.write(w.getHead(corso) + "\n");
			
			if(isDoc && req.getParameterMap().containsKey("mod")){
				out.write("<body onLoad=\"setDisplay('bottone_scissor_right','bottone_scissor_right_hover'); showAll('edit-buttons-testo');\">\n");
			} else {
				out.write("<body>");
			}
			out.write("<a href='./login'><input type='button' id='logoff' alt='logout' title='logout!' /></a>");
			out.write("<div id='main-container'>\n");
			if(isDoc){
			out.write("<a href='./html/aggiunta/pagina.html'><input type='button' id='bottonepage' alt='modifica' title='vai alla aggiunta pagina'/></a>");
		    out.write("<a href='./newlink'><input type='button' id='bottoneLink'  alt='aggiugi link' title='aggiungi link'/></a>");
		    out.write("<input type='button' id='bottone_scissor_left' alt='edita' title='edita' onClick=\"setDisplay('bottone_scissor_left','bottone_scissor_left_hover'); setVisible('edit-buttons-link');\"/>");
		    out.write("<input type='button' id='bottone_scissor_left_hover' alt='edita' title='edita' onClick=\"setDisplay('bottone_scissor_left_hover','bottone_scissor_left'); setHidden('edit-buttons-link');\"/>");
		    out.write("<a href='./html/configurazione/configurazione.html'><input type='button' id='bottoneModifica' alt='modifica' title='vai alla configurazione'/></a>");
		    out.write("<a href='./html/edit/nuovoContenuto.html'><input type='button' id='bottoneAggiungi' alt='aggiugi contenuto' title='aggiungi post'/></a>");
		    out.write("<input type='button' id='bottone_scissor_right' alt='edita' title='edita' onClick=\"setDisplay('bottone_scissor_right','bottone_scissor_right_hover'); showAll('edit-buttons-testo');\"/>");
		    out.write("<input type='button' id='bottone_scissor_right_hover' alt='edita' title='edita' onClick=\"setDisplay('bottone_scissor_right_hover','bottone_scissor_right'); hideAll('edit-buttons-testo');\"/>");
			}
			
			out.write(w.getHeader(corso) + "\n");
			out.write(w.getBar(corso) + "\n");
			out.write("<div id='contents'>\n");
			out.write("<div id='post'>\n");

			String id = req.getParameter("page");
			if (id != null) {
				List<Content> contents;
				try {
					
					contents = Setup.getSetup().getMediator()
							.getpService().getCurrentPageContents(corso,id);
					req.getSession().setAttribute("current", id);
				for (int i = 0; i < contents.size(); i++) {
					Content c=contents.get(i);
					out.write("<div class='tratto'>\n");
					out.write(c.getHTMLCode()+"\n");
					out.write("<div class='edit-buttons-testo'>\n");
					out.write("<a href='./delete?name="+c.getContentName()+"'>");
					out.write("<input type='button' class='little-button-delete' alt='delete' title='delete'/></a>\n");
					out.write("<a href='./mod?name="+c.getContentName()+"&type="+c.getType()+"&ID="+c.getID()+"'>");
					out.write("<input type='button' class='little-button-edit' alt='edita' title='edita'/></a>\n");
					out.write("<a href='./swap?name="+c.getContentName()+"&cmd=up'>");
					out.write("<input type='button' class='button-up' alt='up' title='up'/></a>\n");
					out.write("<a href='./swap?name="+c.getContentName()+"&cmd=down'>");
					out.write("<input type='button' class='button-down' alt='down' title='down'/></a>\n");
					out.write("</div>\n");
					out.write("</div>\n");
				}
				} catch (PageNotFoundException e) {
					resp.sendRedirect("./html/errorpages/pagina404.html");
				}
				
			}else{
				resp.sendRedirect("./view?page=index");
			}
			
			out.write("</div>\n");
			out.write("</div>\n");
			out.write(w.getFooter() + "\n");
			out.write("</div>\n");
			out.write("</body>\n");
			out.write("</html>\n");
			out.close();

		} else {
			resp.sendRedirect("./html/LOGIN/login.html");
		}

	}

}
